package cn.iocoder.yudao.module.promotion.fod.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.MerchantExtensionService;
import cn.iocoder.yudao.framework.business.basic.service.impl.AbstractService;
import cn.iocoder.yudao.framework.common.exception.Assertions;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.api.promotion.fod.bo.FirstOrderDiscountBO;
import cn.iocoder.yudao.module.promotion.fod.convert.FirstOrderDiscountConfigConvert;
import cn.iocoder.yudao.module.promotion.fod.dao.FirstOrderDiscountConfigMapper;
import cn.iocoder.yudao.module.promotion.fod.pojo.po.FirstOrderDiscountConfig;
import cn.iocoder.yudao.module.promotion.fod.pojo.vo.FirstOrderDiscountConfigVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/09/07
 */
@Service
public class FirstOrderDiscountConfigService extends AbstractService<FirstOrderDiscountConfig, FirstOrderDiscountConfigVO> implements MerchantExtensionService<FirstOrderDiscountConfig> {
    @Resource
    private FirstOrderDiscountConfigMapper firstOrderDiscountConfigMapper;

    @Override
    public FirstOrderDiscountConfigMapper getInjectedMapper() {
        return firstOrderDiscountConfigMapper;
    }

    @Override
    public FirstOrderDiscountConfigVO convertToViewObject(FirstOrderDiscountConfig entity) {
        return FirstOrderDiscountConfigConvert.INSTANCE.toViewObject(entity);
    }

    @DataPermission(enable = false)
    @Override
    public FirstOrderDiscountConfig getOrCreate(Long shopId) {
        CoreLambdaQueryWrapper<FirstOrderDiscountConfig> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.eq(FirstOrderDiscountConfig::getShopId, shopId);
        wrapper.orderByDesc(FirstOrderDiscountConfig::getId);
        wrapper.limitN(1);
        FirstOrderDiscountConfig entity = firstOrderDiscountConfigMapper.selectOne(wrapper);
        if (entity == null) {
            entity = new FirstOrderDiscountConfig();
            entity.setShopId(shopId);
            entity.setJson(List.of(new FirstOrderDiscountBO()));
            firstOrderDiscountConfigMapper.insert(entity);
        }
        return entity;
    }

    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(Long shopId, List<FirstOrderDiscountBO> param) {
        FirstOrderDiscountConfig entity = getOrCreate(shopId);
        Assertions.notNull(entity, "配置不存在");
        Assertions.notEmpty(param, "配置规则为空");
        entity.setShopId(shopId);
        // 按金额正序排序
        entity.setJson(param.stream().sorted(Comparator.comparing(FirstOrderDiscountBO::getAmount)).toList());
        return firstOrderDiscountConfigMapper.updateById(entity) > 0;
    }

    @DataPermission(enable = false)
    public List<FirstOrderDiscountBO> getList(Long shopId) {
        FirstOrderDiscountConfig entity = getOrCreate(shopId);
        Assertions.notNull(entity, "配置不存在");
        return entity.getJson();
    }

}
